const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pullDown: false, //下拉刷新页面
    commend: [],
    cCommend: false,
    kmapping_id: null,
    user_id: null,
    page: 1,
    initialState: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getUserKmappingTask&token=' + wx.getStorageSync('token'),
      dataType: 'json',
      method: 'GET',
      data: {
        num: 10,
        type: 0,
        learn_status: 2
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _commend = _res.data;
          that.setData({
            'commend': _commend,
            'user_id': _commend[0].user_id,
            'kmapping_id': _commend[0].kmapping_id,
            'initialState': true
          })
        } else {
          that.setData({
            'initialState': true
          })
        }
      }
    })
  },
  getCollected: function(index) {
    var that = this,
      commend = that.data.commend;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=addCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: commend[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          commend[index].is_collection = 1;
          that.setData({
            commend: commend
          })

          wx.showToast({
            title: '收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getunCollected: function(index) {
    var that = this,
      commend = that.data.commend;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=deleteCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: commend[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          commend[index].is_collection = 0;
          that.setData({
            commend: commend
          })
          wx.showToast({
            title: '取消收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  collectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      commend = that.data.commend;
    that.getCollected(index);
  },

  uncollectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      commend = that.data.commend;
    that.getunCollected(index);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    var _this = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getUserKmappingTask?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        page: 1,
        num: 10,
        type: 0,
        learn_status: 2
      },
      success: function(res) {
        var _res = res.data;
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (_res.error_code == 0) {
          var _data = _res.data;
          _this.setData({
            'commend': _data,
            'cCommend': true,
            'page': 1
          })
        }
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var _this = this,
      _page = _this.data.page + 1;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getUserKmappingTask?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        page: _page,
        num: 10,
        type: 0,
        learn_status: 2
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _data = _res.data,
            _commend = _this.data.commend.concat(_data);
          _this.setData({
            'commend': _commend,
            'cCommend': true,
            'page': _page
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})